package com.mxw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mxw.entity.Resource;
import com.mxw.mapper.ResourceMapper;
import com.mxw.service.AWebSpiderService;
import com.mxw.service.WechatMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class MyItemController {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private WechatMpService wechatMpService;

    @PostMapping("/api/wechat")
    @ResponseBody
    public ResponseEntity<?> wechat(@RequestBody Map<String, List<Long>> loginRequest, HttpSession session) {
        Map<String, Object> response = new HashMap<>();

        List<Long> id = loginRequest.get("ids");

        List<Resource> resources = resourceMapper.selectList(new LambdaQueryWrapper<Resource>().in(Resource::getId, id));
        if (resources.size() > 0) {
            wechatMpService.testCreateDraft(resources);
        }

        return ResponseEntity.ok(response);
    }


    @Autowired
    private AWebSpiderService spiderService;

    /**
     * 每四天执行一次爬虫任务
     * cron表达式：秒 分 时 日 月 周
     */
    @PostMapping("/api/spider")
    @ResponseBody
    public ResponseEntity<?> spider(@RequestParam("page") int page) {
        try {
            spiderService.spider(page);
        } catch (Exception e) {
            return ResponseEntity.ok("fail");
        }
        return ResponseEntity.ok("success");
    }

}
